import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { menuPermissionChecker } from '../utils/menu-permission';
import Home from '../views/home.vue';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/dashboard',
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    noAuth: true,
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
            },
            {
                path: '/system-user',
                name: 'system-user',
                meta: {
                    title: '用户管理',
                    permiss: 'system-user',
                },
                component: () => import(/* webpackChunkName: "system-user" */ '../views/system/user.vue'),
            },
            {
                path: '/system-user-group',
                name: 'system-user-group',
                meta: {
                    title: '用户组管理',
                    permiss: 'system-user-group',
                },
                component: () => import(/* webpackChunkName: "system-user-group" */ '../views/system/user-group.vue'),
            },
            {
                path: '/system-menu',
                name: 'system-menu',
                meta: {
                    title: '菜单管理',
                    permiss: 'system-menu',
                },
                component: () => import(/* webpackChunkName: "system-menu" */ '../views/system/menu.vue'),
            },
            {
                path: '/member-list',
                name: 'member-list',
                meta: {
                    title: '会员列表',
                    permiss: 'member-list',
                },
                component: () => import(/* webpackChunkName: "member-list" */ '../views/member/list/index.vue'),
            },
            {
                path: '/prop-list',
                name: 'prop-list',
                meta: {
                    title: '道具列表',
                    permiss: 'prop-list',
                },
                component: () => import(/* webpackChunkName: "prop-list" */ '../views/member/prop/index.vue'),
            },
            {
                path: '/prop-form/:id?',
                name: 'prop-form',
                meta: {
                    title: '新增道具', // Default title
                    permiss: 'prop-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "prop-form" */ '../views/member/prop/form.vue'),
            },
            {
                path: '/level-list',
                name: 'level-list',
                meta: {
                    title: '等级列表',
                    permiss: 'level-list',
                },
                component: () => import(/* webpackChunkName: "level-list" */ '../views/member/level/index.vue'),
            },
            {
                path: '/level-form/:id?',
                name: 'level-form',
                meta: {
                    title: '新增等级', // Default title
                    permiss: 'level-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "level-form" */ '../views/member/level/form.vue'),
            },
            {
                path: '/task-list',
                name: 'task-list',
                meta: {
                    title: '任务列表',
                    permiss: 'task-list',
                },
                component: () => import(/* webpackChunkName: "task-list" */ '../views/member/task/index.vue'),
            },
            {
                path: '/task-form/:id?',
                name: 'task-form',
                meta: {
                    title: '新增任务', // Default title
                    permiss: 'task-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "task-form" */ '../views/member/task/form.vue'),
            },
            {
                path: '/carousel-list',
                name: 'carousel-list',
                meta: {
                    title: '轮播列表',
                    permiss: 'carousel-list',
                },
                component: () => import(/* webpackChunkName: "carousel-list" */ '../views/carousel/list/index.vue'),
            },
            {
                path: '/carousel-form/:id?',
                name: 'carousel-form',
                meta: {
                    title: '新增轮播', // Default title
                    permiss: 'carousel-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "carousel-form" */ '../views/carousel/list/form.vue'),
            },
            {
                path: '/topic-list',
                name: 'topic-list',
                meta: {
                    title: '话题列表',
                    permiss: 'topic-list',
                },
                component: () => import(/* webpackChunkName: "topic-list" */ '../views/carousel/topic/index.vue'),
            },
            {
                path: '/topic-form/:id?',
                name: 'topic-form',
                meta: {
                    title: '新增话题', // Default title
                    permiss: 'topic-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "topic-form" */ '../views/carousel/topic/form.vue'),
            },
            {
                path: '/activity-list',
                name: 'activity-list',
                meta: {
                    title: '活动列表',
                    permiss: 'activity-list',
                },
                component: () => import(/* webpackChunkName: "activity-list" */ '../views/carousel/activity/index.vue'),
            },
            {
                path: '/activity-form/:id?',
                name: 'activity-form',
                meta: {
                    title: '新增活动', // Default title
                    permiss: 'activity-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "activity-form" */ '../views/carousel/activity/form.vue'),
            },
            {
                path: '/faq-list',
                name: 'faq-list',
                meta: {
                    title: '常见问题列表',
                    permiss: 'faq-list',
                },
                component: () => import(/* webpackChunkName: "faq-list" */ '../views/carousel/faq/index.vue'),
            },
            {
                path: '/faq-form/:id?',
                name: 'faq-form',
                meta: {
                    title: '新增问题', // Default title
                    permiss: 'faq-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "faq-form" */ '../views/carousel/faq/form.vue'),
            },
            {
                path: '/sign-in-config',
                name: 'sign-in-config',
                meta: {
                    title: '签到配置',
                    permiss: 'sign-in-config',
                },
                component: () => import(/* webpackChunkName: "sign-in-config" */ '../views/carousel/sign-in-config/index.vue'),
            },
            {
                path: '/service-provider-main',
                name: 'service-provider-main',
                meta: {
                    title: '服务商主体',
                    permiss: 'service-provider-main',
                },
                component: () => import(/* webpackChunkName: "service-provider-main" */ '../views/service-provider/service-provider-main/index.vue'),
            },
            {
                path: '/service-provider-form/:id?',
                name: 'service-provider-form',
                meta: {
                    title: '新增服务商', // Default title
                    permiss: 'service-provider-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "service-provider-form" */ '../views/service-provider/service-provider-main/form.vue'),
            },
            {
                path: '/service-provider-audit/:id/:type',
                name: 'service-provider-audit',
                meta: {
                    title: '服务商审核', // Default title
                    permiss: 'service-provider-audit',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "service-provider-audit" */ '../views/service-provider/service-provider-main/audit.vue'),
            },
            {
                path: '/expert-list',
                name: 'expert-list',
                meta: {
                    title: '专家列表',
                    permiss: 'expert-list',
                },
                component: () => import(/* webpackChunkName: "expert-list" */ '../views/service-provider/expert/index.vue'),
            },
            {
                path: '/expert-audit/:id',
                name: 'expert-audit',
                meta: {
                    title: '专家审核',
                    permiss: 'expert-audit',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "expert-audit" */ '../views/service-provider/expert/audit.vue'),
            },
            {
                path: '/service-list',
                name: 'service-list',
                meta: {
                    title: '服务列表',
                    permiss: 'service-list',
                },
                component: () => import(/* webpackChunkName: "service-list" */ '../views/product/service/index.vue'),
            },
            {
                path: '/service-form/:id?',
                name: 'service-form',
                meta: {
                    title: '服务表单',
                    permiss: 'service-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "service-form" */ '../views/product/service/form.vue'),
            },
            {
                path: '/service-detail/:id/:type',
                name: 'service-detail',
                meta: {
                    title: '服务详情',
                    permiss: 'service-detail',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "service-detail" */ '../views/product/service/detail.vue'),
            },
            {
                path: '/video-image-list',
                name: 'video-image-list',
                meta: {
                    title: '视频图片列表',
                    permiss: 'video-image-list',
                },
                component: () => import(/* webpackChunkName: "video-image-list" */ '../views/product/video-image/index.vue'),
            },
            {
                path: '/video-image-form/:id?',
                name: 'video-image-form',
                meta: {
                    title: '视频图片表单',
                    permiss: 'video-image-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "video-image-form" */ '../views/product/video-image/form.vue'),
            },
            {
                path: '/product-list',
                name: 'product-list',
                meta: {
                    title: '商品列表',
                    permiss: 'product-list',
                },
                component: () => import(/* webpackChunkName: "product-list" */ '../views/product/product/index.vue'),
            },
            {
                path: '/product-form/:id?',
                name: 'product-form',
                meta: {
                    title: '商品表单',
                    permiss: 'product-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "product-form" */ '../views/product/product/form.vue'),
            },
            {
                path: '/product-category-list',
                name: 'product-category-list',
                meta: {
                    title: '商品分类列表',
                    permiss: 'product-category-list',
                },
                component: () => import(/* webpackChunkName: "product-category-list" */ '../views/product/category/index.vue'),
            },
            {
                path: '/product-category-form/:id?',
                name: 'product-category-form',
                meta: {
                    title: '商品分类表单',
                    permiss: 'product-category-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "product-category-form" */ '../views/product/category/form.vue'),
            },
            {
                path: '/interest-list',
                name: 'interest-list',
                meta: {
                    title: '兴趣列表',
                    permiss: 'interest-list',
                },
                component: () => import(/* webpackChunkName: "interest-list" */ '../views/product/interest/index.vue'),
            },
            {
                path: '/interest-form/:id?',
                name: 'interest-form',
                meta: {
                    title: '兴趣表单',
                    permiss: 'interest-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "interest-form" */ '../views/product/interest/form.vue'),
            },
            {
                path: '/expertise-list',
                name: 'expertise-list',
                meta: {
                    title: '擅长领域列表',
                    permiss: 'expertise-list',
                },
                component: () => import(/* webpackChunkName: "expertise-list" */ '../views/product/expertise/index.vue'),
            },
            {
                path: '/expertise-form/:id?',
                name: 'expertise-form',
                meta: {
                    title: '擅长领域表单',
                    permiss: 'expertise-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "expertise-form" */ '../views/product/expertise/form.vue'),
            },
            {
                path: '/question-category-list',
                name: 'question-category-list',
                meta: {
                    title: '快速问题分类列表',
                    permiss: 'question-category-list',
                },
                component: () => import(/* webpackChunkName: "question-category-list" */ '../views/distribution/question-category/index.vue'),
            },
            {
                path: '/question-category-form/:id?',
                name: 'question-category-form',
                meta: {
                    title: '快速问题分类表单',
                    permiss: 'question-category-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "question-category-form" */ '../views/distribution/question-category/form.vue'),
            },
            {
                path: '/recommended-question-list',
                name: 'recommended-question-list',
                meta: {
                    title: '推荐问题列表',
                    permiss: 'recommended-question-list',
                },
                component: () => import(/* webpackChunkName: "recommended-question-list" */ '../views/distribution/recommended-question/index.vue'),
            },
            {
                path: '/recommended-question-form/:id?',
                name: 'recommended-question-form',
                meta: {
                    title: '推荐问题表单',
                    permiss: 'recommended-question-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "recommended-question-form" */ '../views/distribution/recommended-question/form.vue'),
            },
            {
                path: '/distribution-task-list',
                name: 'distribution-task-list',
                meta: {
                    title: '任务列表',
                    permiss: 'distribution-task-list',
                },
                component: () => import(/* webpackChunkName: "task-list" */ '../views/distribution/task/index.vue'),
            },
            {
                path: '/distribution-task-form/:id?',
                name: 'distribution-task-form',
                meta: {
                    title: '任务表单',
                    permiss: 'distribution-task-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "task-form" */ '../views/distribution/task/form.vue'),
            },
            {
                path: '/withdrawal-list',
                name: 'withdrawal-list',
                meta: {
                    title: '提现列表',
                    permiss: 'withdrawal-list',
                },
                component: () => import(/* webpackChunkName: "withdrawal-list" */ '../views/distribution/withdrawal/index.vue'),
            },
            {
                path: '/withdrawal-audit/:id',
                name: 'withdrawal-audit',
                meta: {
                    title: '提现审核',
                    permiss: 'withdrawal-audit',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "withdrawal-audit" */ '../views/distribution/withdrawal/audit.vue'),
            },
            {
                path: '/platform-withdrawal-list',
                name: 'platform-withdrawal-list',
                meta: {
                    title: '提现列表(平台)',
                    permiss: 'platform-withdrawal-list',
                },
                component: () => import(/* webpackChunkName: "platform-withdrawal-list" */ '../views/distribution/platform-withdrawal/index.vue'),
            },
            {
                path: '/question-order-list',
                name: 'question-order-list',
                meta: {
                    title: '提问订单',
                    permiss: 'question-order-list',
                },
                component: () => import(/* webpackChunkName: "question-order-list" */ '../views/order/question-order/index.vue'),
            },
            {
                path: '/question-order-detail/:id',
                name: 'question-order-detail',
                meta: {
                    title: '提问订单详情',
                    permiss: 'question-order-detail',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "question-order-detail" */ '../views/order/question-order/detail.vue'),
            },
            {
                path: '/service-order-list',
                name: 'service-order-list',
                meta: {
                    title: '服务订单',
                    permiss: 'service-order-list',
                },
                component: () => import(/* webpackChunkName: "service-order-list" */ '../views/order/service-order/index.vue'),
            },
            {
                path: '/service-order-detail/:id',
                name: 'service-order-detail',
                meta: {
                    title: '服务订单详情',
                    permiss: 'service-order-detail',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "service-order-detail" */ '../views/order/service-order/detail.vue'),
            },
            {
                path: '/product-order-list',
                name: 'product-order-list',
                meta: {
                    title: '商品订单',
                    permiss: 'product-order-list',
                },
                component: () => import(/* webpackChunkName: "product-order-list" */ '../views/order/product-order/index.vue'),
            },
            {
                path: '/product-order-detail/:id',
                name: 'product-order-detail',
                meta: {
                    title: '商品订单详情',
                    permiss: 'product-order-detail',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // Mark for dynamic title
                },
                component: () => import(/* webpackChunkName: "product-order-detail" */ '../views/order/product-order/detail.vue'),
            },
            {
                path: '/member-detail/list/:id',
                name: 'member-detail',
                meta: {
                    title: '会员详情',
                    permiss: 'member-detail',
                    hidden: true, // 隐藏菜单
                },
                component: () => import(/* webpackChunkName: "member-detail" */ '../views/member/list/detail/index.vue'),
            },
            {
                path: '/coupon-list',
                name: 'coupon-list',
                meta: {
                    title: '优惠券列表',
                    permiss: 'coupon-list',
                },
                component: () => import(/* webpackChunkName: "coupon-list" */ '../views/member/coupon/index.vue'),
            },
            {
                path: '/coupon-form/:id?',
                name: 'coupon-form',
                meta: {
                    title: '新增优惠券',
                    permiss: 'coupon-form',
                    hidden: true, // 隐藏菜单
                    dynamicTitle: true, // 标记为动态标题
                },
                component: () => import(/* webpackChunkName: "coupon-form" */ '../views/member/coupon/form.vue'),
            },
            {
                path: '/icon',
                name: 'icon',
                meta: {
                    title: '图标',
                    permiss: 'icon',
                },
                component: () => import(/* webpackChunkName: "icon" */ '../views/pages/icon.vue'),
            },
            // {
            //     path: '/editor',
            //     name: 'editor',
            //     meta: {
            //         title: '富文本编辑器',
            //         permiss: 'editor',
            //     },
            //     component: () => import(/* webpackChunkName: "editor" */ '../views/pages/editor.vue'),
            // },
            // {
            //     path: '/theme',
            //     name: 'theme',
            //     meta: {
            //         title: '主题设置',
            //         permiss: 'theme',
            //     },
            //     component: () => import(/* webpackChunkName: "theme" */ '../views/pages/theme.vue'),
            // },
            {
                path: '/403',
                meta: {
                    title: '没有权限',
                    noAuth: true,
                },
                component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
            },
        ],
    },
    {
        path: '/login',
        meta: {
            title: '登录',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/pages/login.vue'),
    },
    {
        path: '/register',
        meta: {
            title: '注册',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "register" */ '../views/pages/register.vue'),
    },
    {
        path: '/reset-pwd',
        meta: {
            title: '重置密码',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "reset-pwd" */ '../views/pages/reset-pwd.vue'),
    },
    {
        path: '/404',
        meta: {
            title: '找不到页面',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "404" */ '../views/pages/404.vue'),
    },
    { path: '/:path(.*)', redirect: '/404' },
];

// 动态标题配置
const dynamicTitleConfig = [
    {
        path: '/coupon-form',
        addTitle: '新增优惠券',
        editTitle: '编辑优惠券'
    },
    {
        path: '/prop-form',
        addTitle: '新增道具',
        editTitle: '编辑道具'
    },
    {
        path: '/level-form',
        addTitle: '新增等级',
        editTitle: '编辑等级'
    },
    {
        path: '/task-form',
        addTitle: '新增任务',
        editTitle: '编辑任务'
    },
    {
        path: '/carousel-form',
        addTitle: '新增轮播',
        editTitle: '编辑轮播'
    },
    {
        path: '/topic-form',
        addTitle: '新增资料',
        editTitle: '编辑资料'
    },
    {
        path: '/activity-form',
        addTitle: '添加活动',
        editTitle: '编辑活动'
    },
    {
        path: '/faq-form',
        addTitle: '新增问题',
        editTitle: '编辑问题'
    },
    {
        path: '/service-provider-form',
        addTitle: '新增服务商',
        editTitle: '编辑服务商'
    }
];

// 特殊路径标题配置
const specialTitleConfig = [
    {
        path: '/service-provider-audit',
        type: 'organization',
        title: '机构审核'
    },
    {
        path: '/service-provider-audit',
        type: 'supplier',
        title: '供应商审核'
    }
];

/**
 * 动态设置页面标题
 * @param to 路由对象
 */
const setDynamicTitle = (to: any) => {
    // 检查动态标题配置
    for (const config of dynamicTitleConfig) {
        if (to.path === config.path && !to.params.id) {
            to.meta.title = config.addTitle;
            return;
        } else if (to.path.startsWith(config.path + '/') && to.params.id) {
            to.meta.title = config.editTitle;
            return;
        }
    }

    // 检查特殊路径标题配置
    for (const config of specialTitleConfig) {
        if (to.path.startsWith(config.path) && to.params.type === config.type) {
            to.meta.title = config.title;
            return;
        }
    }
};

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();

    console.log('路由跳转:', to.path);

    // 跳过权限检查的页面
    if (to.meta.noAuth === true) {
        next();
        return;
    }

    // 检查菜单权限
    if (!menuPermissionChecker.hasPermission(to.path)) {
        console.log('权限不足，跳转到403页面:', to.path);
        next('/403');
        return;
    }

    // 获取菜单信息并设置页面标题
    const menuInfo = menuPermissionChecker.getMenuInfo(to.path);
    if (menuInfo && menuInfo.menuName) {
        to.meta.title = menuInfo.menuName;
    }

    // 动态设置页面标题
    setDynamicTitle(to);
    
    next();
});

router.afterEach((to) => {
    NProgress.done();
    // 更新页面标题
    if (to.meta.title) {
        document.title = to.meta.title as string;
    }
});

export default router;
